package val.to.hardware;

import android.content.Context;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class DeviceInfo {

    private static DeviceInfo deviceInfo = null;

    private ConnectivityManager connectivity;
    private TelephonyManager telephonyManager;
    public NetworkInfo mWifiInfo, mMobileInfo;
    private MyPhoneStateListener phoneStateListener = null;
    private Location mLocation = null;
    private int mSignalStrength = -1;

    private DeviceInfo() {
    	phoneStateListener = new MyPhoneStateListener();
    }

    public static synchronized DeviceInfo getInstance() {
        if (deviceInfo == null) {
            deviceInfo = new DeviceInfo();
        }
        return deviceInfo;
    }

    public void init(Context context) {
        connectivity = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        
        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTH);
    }
    
    public static String getPhoneModel() {
    	return Build.MODEL;
    }
    
    public static String getAndroidVersion() {
    	return Build.VERSION.RELEASE;
    }

    public boolean isNetworkAvailable() {
        synchronized (this) {            
            mWifiInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            mMobileInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mMobileInfo.isConnected() || mWifiInfo.isConnected()) {
                return true;
            }
            return false;
        }
    }

	public void setLocation(Location location) {
		synchronized (this) {
			this.mLocation = new Location(location);
		}		
	}
	
	public Location getLocation() {
		synchronized (this) {
			return mLocation;
		}
	}

	
	public String getCarrierName() {
		synchronized (this) {
			String carrier = telephonyManager.getNetworkOperatorName();
			if (carrier == null || carrier.length() == 0)
				return "SIM card is not available";
			return carrier;
		}
	}
	
	public int getSignalStrength() {
		synchronized (this) {
			return mSignalStrength;
		}
	}
	
	public void stopPhoneStateListner() {
		if (telephonyManager != null) {
			telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
		}
	}
	
	public String getNetworkType() {
		synchronized (this) {
			mWifiInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            mMobileInfo = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            if (mWifiInfo.isConnected()) {
                return "WiFi";
            }
            if (mMobileInfo.isConnected()) {
				switch (telephonyManager.getNetworkType()) {
				case TelephonyManager.NETWORK_TYPE_1xRTT:
					return "1xRTT";			
				case TelephonyManager.NETWORK_TYPE_CDMA:
					return "CDMA";
				case TelephonyManager.NETWORK_TYPE_EDGE:
					return "EDGE";
				/* case TelephonyManager.NETWORK_TYPE_EHRPD:	// api level 11
					return "eHRPD"; */
				case TelephonyManager.NETWORK_TYPE_EVDO_0:
					return "EVDO revision 0";
				case TelephonyManager.NETWORK_TYPE_EVDO_A:
					return "EVDO revision A";
				/* case TelephonyManager.NETWORK_TYPE_EVDO_B:	// api level 9
					return "EVDO revision B"; */
				case TelephonyManager.NETWORK_TYPE_GPRS:
					return "GPRS";
				case TelephonyManager.NETWORK_TYPE_HSDPA:
					return "HSDPA";
				case TelephonyManager.NETWORK_TYPE_HSPA:
					return "HSPA";
				/* case TelephonyManager.NETWORK_TYPE_HSPAP:
					return "HSPA+"; */				
				case TelephonyManager.NETWORK_TYPE_HSUPA:
					return "HSUPA";
				case TelephonyManager.NETWORK_TYPE_IDEN:
					return "iDen";
				/* case TelephonyManager.NETWORK_TYPE_LTE:
					return "LTE";			
				} */
				case TelephonyManager.NETWORK_TYPE_UNKNOWN:
					return "Unknown";
				default:
					return "Not detactable";
				}
            }
            return "Error detecting";
		}
	}
	
	private class MyPhoneStateListener extends PhoneStateListener {
		@Override
		public void onSignalStrengthChanged(int asu) {			
			super.onSignalStrengthChanged(asu);
			mSignalStrength = asu;
		}		
	}
}
